<form action="" class="form-horizontal" role="form">

  <div class="form-group">
    <label class="col-sm-2 control-label">网页标题</label>
    <div class="col-sm-3">
      <input type="text" class="form-control" name="page_title" placeholder="请输入名称" datatype="*" value="{$productGoodsConfig.page_title}"/>
    </div>
  </div>
  
  <div class="form-group">
    <label class="col-sm-2 control-label">网页关键词</label>
    <div class="col-sm-3">
       <input type="text" class="form-control" name="page_keywords" placeholder="请输入名称" value="{$productGoodsConfig.page_keywords}"/>
    </div>
  </div>
  
  <div class="form-group">
    <label class="col-sm-2 control-label">网页描述</label>
    <div class="col-sm-4">
      <textarea  class="form-control" rows="8" cols="" name="page_description">{$productGoodsConfig.page_description}</textarea>
    </div>
  </div>

  <div class="form-group">
    <label class="col-sm-2 control-label">分成方式</label>
    <div class="col-sm-4">
       {$productGoodsConfig.sharingMethodOptions}
    </div>
  </div>
  
  <div class="form-group none" id="sharing_method_div">
		<label class="col-sm-2 control-label">分成设置<br/>
		<span class="label label-info">商品分成比例和为10（合作商:所有者:销售者）</span>
        </label>
		<div class="col-sm-4 productPercent">
			<input type="number" class="form-control" name="productGoodsConfig[percent][0]" placeholder="合作商"
				value="{$productGoodsConfig[percent][0]*1}" style="float:left;width:92px;" min="0" max="10" /> <span style="float: left; margin: 0px 3px; margin-top: 6px;">:</span>
			<input type="number" class="form-control" name="productGoodsConfig[percent][1]" placeholder="客户"
				value="{$productGoodsConfig[percent][1]*1}" style="float:left;width:92px;" min="0" max="10"/> <span style="float: left; margin: 0px 3px; margin-top: 6px;">:</span>
			<input type="number" class="form-control" name="productGoodsConfig[percent][2]" placeholder="销售者"
				value="{$productGoodsConfig[percent][2]*1}" style="float:left;width:92px;" min="0" max="10"/>
		</div>
  </div>
  
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-8">
      <a class="btn btn-default" onclick="javascript:history.go(-1)">返回上页</a>
	  <a class="btn btn-default" onclick="self.location=document.referrer">返回上页并刷新</a>
      <input type="hidden" name="id" value="{$productGoodsConfig.id}"/>
      <button type="submit" class="btn btn-primary submit" data-loading-txt="提交中，请稍后……" data-ajax-url="__CONTROLLER__/save">保存更改</button>
    </div>
  </div>
</form>

<script type="text/javascript">
$(function() {
	$("input[name='sharing_method']").click(function() {
		if( $(this).val()==1 ) {
			$("#sharing_method_div input").attr("datatype", "*");
		    $("#sharing_method_div").show();
		} else {
			$("#sharing_method_div input").removeAttr("datatype");
			$("#sharing_method_div").hide();
		}
	});
	$("input[name='sharing_method']:checked").click();
	
	window.PercentCtl = {
		'percent' : [],
		'init' : function() {
			$(".productPercent input").each(function() {
				PercentCtl.percent.push($(this).val());
			});
			$(".productPercent input").change(function() {
				var percent = PercentCtl.percent;
				var i = $(".productPercent input").index(this);
				var currentValue = $(this).val()*1;
				if( currentValue > percent[i]*1 ) {
					var inc = currentValue*1 - percent[i]*1;
					var m = [];
					var t = 0;
					for(var j=i+1; j<percent.length; j++) {
					   t += percent[j]*1;
					   if ( t > inc ) {
						   m[j] = t - inc;
						   break;
					   } else if( t <= inc ) {
						   m[j] = 0;
					   }
					}
					if(t >= inc) {
						for (var o in m) PercentCtl.percent[o] = m[o]; 
						PercentCtl.percent[i] = $(this).val();
					}
				} else if( currentValue < percent[i] ) {
					var dec = percent[i]*1 - currentValue*1;
					var j = i+1;
					if ( j < percent.length ) {
						PercentCtl.percent[j] = PercentCtl.percent[j]*1+dec;
						PercentCtl.percent[i] = $(this).val();
					}
				}
				PercentCtl.updateData();
			});
		},
		'updateData' : function() {
			$(".productPercent input").each(function(i) {
				$(this).val(PercentCtl.percent[i]*1);
			});
		}
	};
	PercentCtl.init();
});
</script>